<div class="page-body">
    <span class="btn btn-default purple" href="javascript:void(0)" id="0" up-id="0" onclick="add(event)"><i class="fa fa-plus"></i>
        新增顶级分类
    </span>
    <div class="row">
        <volist name="data" id="vo">
            <div class="col-lg-6 col-sm-6 col-xs-12">
                <h5 class="row-title"  id="{$vo.type_id}" onclick="edit(event)"><i class="typcn typcn-th-menu"></i>{$vo.name}</h5>
                <div class="dd">
                    <ol class="dd-list">
                        <volist name="vo.child" id="v">
                            <li class="dd-item bordered-palegreen" id="{$v.type_id}" onclick="edit(event)">
                                <div class="dd-handle">
                                    {$v.type_id}.名称:{$v.name}
                                    <if condition="$v.is_recommend eq '1'">
                                        <span class="label label-darkorange pull-right">已推荐</span>
                                    </if>
                                </div>
                            </li>
                        </volist>
                        <li class="dd-item bordered-palegreen" id="up{$vo.type_id}" up-id="{$vo.type_id}" onclick="add(event)">
                            <div class="dd-handle btn btn-default purple bg-warning" href="javascript:void(0);" style="margin-bottom: 15px;"><i class="fa fa-plus"></i>
                                新增子分类
                            </div>
                        </li>
                    </ol>
                </div>
            </div>
        </volist>
    </div>
</div>
<!-- /Page Body -->
<div id="popover88" style="display:none;">
    <div class="form-group">
        <label class="">当前id:
            <span class="type_id"></span></label>
    </div>
    <div class="form-group">
        <label class="">名称</label>
        <input type="text" class="form-control" name="type_name" placeholder="分类名">
    </div>
    <div class="form-group">
        <label>
            <input type="checkbox" class="colored-danger" name="is_recommend">
            <span class="text">首页推荐</span>
        </label>
    </div>
    <div class="form-group">
        <div>
            <button class="btn btn-palegreen" onclick="save()">保存</button>
            <button class="btn btn-danger" onclick="del()">删除</button>
        </div>
    </div>
</div>
<div id="popover99" style="display:none;">
    <div class="form-group">
        <label class="">名称</label>
        <input type="hidden" class="form-control" name="up_id">
        <input type="text" class="form-control" name="type_name" placeholder="分类名">
    </div>
    <div class="form-group">
        <label>
            <input type="checkbox" class="colored-danger" name="is_recommend">
            <span class="text">首页推荐</span>
        </label>
    </div>
    <div class="form-group">
        <div>
            <button class="btn btn-palegreen" onclick="addType()">添加</button>
        </div>
    </div>
</div>
<script>
    var id;
    var did;
    var price_rent;
    var price_buy;
    //显示框
    function edit(event) {
        var object=event.currentTarget ;

        $('.bg-darkorange').removeClass('bg-darkorange');
        $(object).find('.dd-handle').addClass('bg-darkorange');
        //清除旧
        did_n = $(object).attr('id');
        if(did==undefined){
            did=did_n;
        }
        $('#' + did).popover('destroy');
        did = did_n;
        $.post("{:U('getType')}",{"type_id":did},function(data){
            if(data.status=='success'){
                //启用
                var options = {
                    title: '名称修改',
                    placement:'bottom',
                    content: ''
                };
                $('#' + did).popover(options);
                $('#' + did).popover('show');
                $('.popover-content').html(set());
                $('.popover-content .type_id').html(data.data.type_id);
                $('.popover-content [name="type_name"]').val(data.data.name);
                if(data.data.is_recommend=='1'){
                    $('.popover-content [name="is_recommend"]').attr('checked',true);
                }else{
                    $('.popover-content [name="is_recommend"]').attr('checked',false);
                }
            }else{
                alert(data.data);
            }
        },"json");
    }
    //显示框
    function add(event) {
        var object=event.currentTarget ;

        $('.bg-darkorange').removeClass('bg-darkorange');
//        $(object).find('.dd-handle').addClass('bg-darkorange');
        //清除旧
        did_n = $(object).attr('id');
        up_id = $(object).attr('up-id');
        if(did==undefined){
            did=did_n;
        }
        $('#' + did).popover('destroy');
        did = did_n;
        var options = {
            title: '添加分类',
            placement:'bottom',
            content: ''
        };
        $('#' + did).popover(options);
        $('#' + did).popover('show');
        $('.popover-content').html(set_add());
        $('.popover-content [name="up_id"]').val(up_id);
        $('.popover-content [name="type_name"]').val('');
    }

    function set(){
        return $('#popover88').html();
    }

    function set_add(){
        return $('#popover99').html();
    }

    function save(){
        type_id=$('.popover-content .type_id').html();
        type_name=$('.popover-content [name="type_name"]').val();
        if($('.popover-content [name="is_recommend"]').is(':checked')){
            is_recommend=1;
        }else{
            is_recommend=0;
        }
        $.post("{:U('BookType/dosave')}",{'type_id':type_id,'type_name':type_name,'is_recommend':is_recommend},function(data){
            if(data.status=='success'){
                alert(data.data);
            }else{
                alert(data.data);
            }
            location.reload();
        },'json');
    }

    function addType(){
        up_id=$('.popover-content [name="up_id"]').val();
        type_name=$('.popover-content [name="type_name"]').val();
        if($('.popover-content [name="is_recommend"]').is(':checked')){
            is_recommend=1;
        }else{
            is_recommend=0;
        }
        log(is_recommend);
        $.post("{:U('BookType/doadd')}",{'up_id':up_id,'type_name':type_name,'is_recommend':is_recommend},function(data){
            if(data.status=='success'){
                alert(data.data);
                location.reload();
            }else{
                alert(data.data);
            }
        },'json');
    }

    function del(){
        type_id=$('.popover-content .type_id').html();
        $.post("{:U('BookType/dodel')}",{'type_id':type_id},function(data){
            if(data.status=='success'){
                alert(data.data);
            }else{
                alert(data.data);
            }
            location.reload();
        },'json');
    }
</script>